﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LotteryVote.Entity;
using System.Data.SqlClient;
using System.Data.Common;

namespace LotteryVote.Data
{
    public class RoleDataAccess : DataBase
    {
        public RoleDataAccess() : base(DBManager.ConnectionString, DBManager.DBProviderName) { }

        public void Insert(Role role)
        {
            string sql = string.Format(@"Insert into {0} ({1}) values(@RoleName) SELECT IDENT_CURRENT('{0}')", Role.TABLENAME, Role.ROLENAME);
            object roleId = base.ExecuteScalar(CommandType.Text, sql, new SqlParameter(Role.ROLENAME, role.RoleName));
            role.RoleId = Convert.ToInt32(roleId);
        }
        public void Delete(int roleId)
        {
            string sql = string.Format(@"delete {0} where {1}=@{1}", Role.TABLENAME, Role.ROLEID);
            base.ExecuteNonQuery(sql, new SqlParameter(Role.ROLEID, roleId));
        }
        public void Delete(string roleName)
        {
            string sql = string.Format(@"delete {0} where {1}=@{1}", Role.TABLENAME, Role.ROLENAME);
            base.ExecuteNonQuery(sql, new SqlParameter(Role.TABLENAME, roleName));
        }
        public void Update(Role role)
        {
            string sql = string.Format(@"update {0} set {1}=@{1} where {2}=@{2}", Role.TABLENAME, Role.ROLENAME, Role.ROLEID);
            base.ExecuteNonQuery(sql, new SqlParameter(Role.ROLENAME, role.RoleName), new SqlParameter(Role.ROLEID, role.RoleId));
        }

        public Role GetRoleById(int roleId)
        {
            string sql = string.Format(@"select * from {0} where {1}=@{1}", Role.TABLENAME, Role.ROLEID);
            return base.ExecuteEntity<Role>(sql, new SqlParameter(Role.ROLEID, roleId));
        }
        public IList<Role> GetRoles()
        {
            string sql = @"select * from tb_Role";
            return base.ExecuteList<Role>(CommandType.Text, sql);
        }
        public IList<Role> GetByCondition(string condition)
        {
            string sql = "SELECT * FROM TB_ROLE WHERE " + condition;
            return base.ExecuteList<Role>(CommandType.Text, sql);
        }
        public Role GetRoleByCondition(string condition)
        {
            string sql = "SELECT * FROM TB_ROLE WHERE " + condition;
            return base.ExecuteEntity<Role>(CommandType.Text, sql);
        }
    }
}
